#include "XMLParser.h"
#include "OverlapChecker.h"
#include "Constants.h"
#include "Circle.h"
#include "Triangle.h"
#include "Square.h"
#include "Segment.h"
#include "Rectangle.h"
#include "DomainBuilder.h"
#include "Stage.h"


int main(int sruc, char* args[]){
		
	int parseResult;
	Document doc;
	Document defMandatory;
	Document defOptional;
	//vectorList matches;
	initDocument(&defMandatory,255);
	initDocument(&defOptional,255);
	initDocument(&doc,255);

	//----------------------------------
	//Tiempo de parseo.
	//----------------------------------
	
	parseResult = parseFile("parserdefmandatory.xml", &defMandatory, NULL, NULL);
	parseResult = parseFile("parserdefoptional.xml", &defOptional, NULL, NULL);
	parseResult = parseFile("figuras.xml", &doc, &defMandatory, &defOptional);

    if (parseResult != OPERATION_FATAL){
        printDocument(&doc);
        //matches = checkOverlaps(&doc);
        //displayMatches(matches);

		DomainBuilder* builder;
		builder = DomainBuilder::getInstance();

		//----------------------------------
		//Tiempo de Buildeo.
		//----------------------------------

		//Levanta del xml los valores del Settings
		Settings* general = builder->buildSettingsFromDocument(&doc);

		//Levanta del xml todas las texturas
		vector<Texture*> textures = builder->buildTexturesFromDocument(&doc);

		//Levanta del xml todas las Shapes.
		vector<Shape*> shapes = builder->buildShapesFromDocument(&doc);

    } else {
        printf("Se ha detectado un error fatal, se detiene el parsing.\n");
    }

	//----------------------------------
	//Tiempo de construccion de Escenario
	//----------------------------------

	Stage* stage = Stage::getInstance();


	SDL_Surface *screen;
    

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("Error al iniciar SDL\n");
        return 1;
    }
    SDL_WM_SetCaption("Taller Maradona - Figuras", "Taller Maradona - Figuras");
    atexit(SDL_Quit);
    
	//Obtenemos la resolucion
	Point* resolution = stage->resolveResolution();
 	int screenWidth = resolution->getX();
	int screenHeight = resolution->getY();

	screen = SDL_SetVideoMode(screenWidth,screenHeight, Stage::PIXEL_THICKNESS, SDL_SWSURFACE);
    if (screen == NULL) {
        printf("Error al setear video:%s\n", SDL_GetError());
        return 1;
    }
	
	// construir el modelo de objetos
	DomainBuilder* db = DomainBuilder::getInstance();
	vector<Shape*> shapes = db->buildShapesFromDocument(&doc);
	vector<Texture*> textures = db->buildTexturesFromDocument(&doc);
	
	// construir el escenario
	stage->setShapes(shapes);
	stage->setTextures(textures);
	
	//Dibujar el escenario
	stage->draw(screen);

	// seguir mostrando la pantalla hasta que se apriete una tecla
	int done=0;
	SDL_Event event;
	while(done == 0) {
		while ( SDL_PollEvent(&event) ) {
		if ( event.type == SDL_KEYDOWN )
		done = 1;
		}
	}
    SDL_Quit();        

    return 0;

}

